using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Entities;
using System.Configuration;

namespace DAL
{
   public  class tbTrangThaiDAL
    {
        private SqlConnection cn = null;
        public tbTrangThaiDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbTrangThaiDAL(tbTrangThaiDAL dao)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbTrangThai trangthai)
        {
            SqlCommand cm = new SqlCommand("tbTrangThai_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Ten", trangthai.Ten);     
            cm.ExecuteNonQuery();
        }
        public void Update(tbTrangThai trangthai)
        {
            SqlCommand cm = new SqlCommand("tbTrangThai_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTT", trangthai.MaTT);
            cm.Parameters.AddWithValue("@Ten", trangthai.Ten);            
            cm.ExecuteNonQuery();
        }
        public void Delete(int ID)
        {
            SqlCommand cm = new SqlCommand("tbTrangThai_Detele", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTT", ID);
            cm.ExecuteNonQuery();

        }
       public tbTrangThaiCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("tbTrangThai_GET", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Where", where);
           cm.Parameters.AddWithValue("@PageIndex", pageIndex);
           cm.Parameters.AddWithValue("@PageSize", pageSize);
           cm.Parameters.AddWithValue("@OrderBy", orderBy);
           cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
           cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
           SqlDataReader dr = cm.ExecuteReader();
           tbTrangThaiCollection trangthaiCollection = new tbTrangThaiCollection();
           while (dr.Read())
           {
               tbTrangThai trangthai = new tbTrangThai();
               trangthai .MaTT  = int.Parse(dr.GetValue(0).ToString());
               trangthai .Ten  = dr.GetValue(1).ToString();
               trangthaiCollection.Add(trangthai);
           }
           dr.Close();
           totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
           return trangthaiCollection;


       }
    }
}
